package com.aorise.service.order.impl;

import com.aorise.exception.SystemException;
import com.aorise.mapper.order.RepairCameraMapper;
import com.aorise.model.map.CameraInfoVo;
import com.aorise.model.order.WorkLogModel;
import com.aorise.model.order.WorkOrderInfo;
import com.aorise.model.order.WorkOrderModel;
import com.aorise.model.order.WorkRepairCameraVO;
import com.aorise.service.order.RepairCameraService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.DisposeTypeEnum;
import com.aorise.utils.WorkOrderEnum;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/10
 * @Description:
 */
@Service
public class RepairCameraServiceImpl implements RepairCameraService{

    @Autowired
    private RepairCameraMapper repairCameraMapper;

    /**
     * 获取需维修设备列表
     * @param maintUser  运维人员
     * @param caName    设备名称
     * @param exception 异常类型
     * @param group     分组
     * @return  需维修设备列表
     */
    @Override
    public List<WorkRepairCameraVO> getRepairCamera(Integer maintUser,String caName,String exception,String group) {
        Map map = Maps.newHashMap();
        String[] exceptionSplit =null;
        if(StringUtils.isNotBlank(exception)){
            exceptionSplit = exception.split(",");
        }
        map.put("maintUser",maintUser);
        map.put("caName",caName);
        map.put("exceptionSplit",exceptionSplit);
        map.put("group",group);
        return repairCameraMapper.getRepairCamera(map);
    }

    /**
     * 运维工单详情
     * @param orderId 工单ID
     * @return
     */
    @Override
    public WorkOrderInfo getWorkOrderInfo(Integer orderId) {
        Map map = Maps.newHashMap();
        map.put("orderId",orderId);
        map.put("state", ConstDefine.STATE_ABLE);
        WorkOrderInfo workOrderInfo =repairCameraMapper.getWorkOrderInfo(map);
        return workOrderInfo;
    }

    /**
     * 修改工单状态
     * @param orderId   工单ID
     * @param orderState    工单状态
     * @param disposeUser   处理人ID
     * @param logDescribe   处理说明
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = { Exception.class })
    @Override
    public boolean upWorkOderstate(Integer orderId, Integer orderState,Integer disposeUser,String logDescribe,String finishTime) {

        Integer loginUserId = getLoginUser().getId();
        Map map = Maps.newHashMap();
        map.put("orderId",orderId);
        map.put("orderState",getWorkStatus(orderState));
        map.put("state", ConstDefine.STATE_ABLE);
        map.put("disposeUser", disposeUser);

        /**通过工单ID 查询工单*/
        WorkOrderModel workOrder =repairCameraMapper.selectWorkById(map);
        if(workOrder ==null){
            throw new SystemException("该工单状态已被撤回,请刷新页面重试");
        }
        DisposeTypeEnum enumType = DisposeTypeEnum.getEnumType(orderState);
        switch (enumType){
            case REASSIGNMENT:
                if(WorkOrderEnum.SENDBACK.getEn() !=workOrder.getWstatus()){
                    throw new SystemException("该工单状态已被更改,请刷新页面重试");
                }
                break;
            case ORDERRECEIVING:
            case SENDBACK:
                if(WorkOrderEnum.WAITLIST.getEn() !=workOrder.getWstatus()){
                    throw new SystemException("该工单状态已被更改,请刷新页面重试");
                }
                break;
            case TOBECONFIRMED:
                if(WorkOrderEnum.BEINGPRO.getEn() !=workOrder.getWstatus()){
                    throw new SystemException("该工单状态已被更改,请刷新页面重试");
                }
                break;
            case NOTPASS:
            case ACCOMPLISH:
                if(WorkOrderEnum.CONFIRMED.getEn() !=workOrder.getWstatus()){
                    throw new SystemException("该工单状态已被更改,请刷新页面重试");
                }
                break;
        }






        /**改派的时候修改数据 */
        if(orderState == DisposeTypeEnum.REASSIGNMENT.getEn()){
            map.put("finishTime", finishTime);
        }
        /**通过完成工单的时候修改数据 */
        if(orderState == DisposeTypeEnum.ACCOMPLISH.getEn()){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            Date d = new Date();
            map.put("realFinishTime", sdf.format(d));
            /** 视频设备异常 正常 */
            map.put("exception",ConstDefine.EXCEPTION);
            map.put("online",ConstDefine.CAMERA_ONLINE);
            /**修改设备状态 */
            repairCameraMapper.upCameraState(map);
        }
        WorkLogModel workLog = WorkLogModel.builder().workId(orderId)
                .disposeUser(disposeUser).disposeType(orderState)
                .logDescribe(logDescribe).createUser(loginUserId)
                .build();
        /**添加工单修改记录 */
        repairCameraMapper.insertworkLog(workLog);
        return getBoolean(repairCameraMapper.upWorkOderstate(map));
    }

    /**
     * 添加工单关注
     * @param orderId   工单ID
     * @return
     */
    @Override
    public boolean addFocus(Integer orderId) {
        Map map =Maps.newHashMap();
        map.put("orderId", orderId);
        map.put("loginUserId", getLoginUser().getId());
        return getBoolean(repairCameraMapper.addFocus(map));
    }

    /**
     * 取消工单关注
     * @param orderId   工单ID
     * @return
     */
    @Override
    public boolean delFocus(Integer orderId) {
        Map map =Maps.newHashMap();
        map.put("orderId", orderId);
        map.put("loginUserId", getLoginUser().getId());
        return getBoolean(repairCameraMapper.delFocus(map));
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = { Exception.class })
    @Override
    public boolean delWorkOrder(Integer orderId) {
        Map map =Maps.newHashMap();
        map.put("orderId", orderId);
        map.put("state", ConstDefine.STATE_ABLE);
        /**通过工单ID 查询工单*/
        WorkOrderModel workOrder =repairCameraMapper.selectWorkById(map);
        if( workOrder !=null && WorkOrderEnum.WAITLIST.getEn() !=workOrder.getWstatus()){
            throw new SystemException("该工单状态已被更改,请刷新页面重试");
        }
        /**删除工单设备记录 */
        map.put("state", ConstDefine.STATE_DISABLE);
        repairCameraMapper.delworkCamera(map);
        /**撤回工单 */
        return getBoolean(repairCameraMapper.delWorkOrder(map));
    }

    /**
     * 获取地图设备数量
     * @return
     */
    @Override
    public Map getMapInfo() {
        Map map =Maps.newHashMap();
        map.put("halt", ConstDefine.HALT_NO);
        map.put("state", ConstDefine.STATE_ABLE);
        return repairCameraMapper.getMapInfo(map);
    }

    /**
     * 电子地图设备信息
     * @return
     */
    @Override
    public List<CameraInfoVo> getMapCameraInfo() {
        Map map =Maps.newHashMap();
        map.put("halt", ConstDefine.HALT_NO);
        map.put("state", ConstDefine.STATE_ABLE);
        return repairCameraMapper.getMapCameraInfo(map);
    }

    /**
     * 工单处理记录处理类型 匹配 工单状态
     * @param disposeType
     * @return
     */
    private Integer getWorkStatus(Integer disposeType){
        for (WorkOrderEnum value:WorkOrderEnum.values()){
            if(disposeType ==value.getEn()){
                return value.getEn();
            }
        }
        return WorkOrderEnum.WAITLIST.getEn();
    }

}
